<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    fullscreen
    @ok="handleOk"
    :okButtonProps="{ style: { display: 'none' } }"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-table
      style="margin-top: 10px"
      ref="table"
      size="middle"
      :scroll="{x:true}"
      bordered
      rowKey="id"
      :columns="detailColumns"
      :dataSource="dataSource"
      :pagination="ipagination"
      :loading="tableLoading"
      class="j-table-force-nowrap1"
      @change="handleTableChange"
    >
      <span slot="action" slot-scope="text, record">
        <a @click="handleDetail(record)">详情</a>
        <a-divider type="vertical" v-if="record.status == 0"/>
        <a-popconfirm
          title="确定撤回吗?"
          v-if="record.status == 0"
          @confirm="() => handleWithdraw(record)">
          <a>撤回</a>
        </a-popconfirm>
      </span>
    </a-table>
    <packaging-work-modal ref="packagingWorkModal" />
  </j-modal>
</template>

<script>

import PackagingWorkForm from './PackagingWorkForm.vue'
import { getAction, postAction } from '@api/manage'
import PackagingWorkModal from './PackagingWorkModal.vue'

export default {
  name: 'PackagingWorkRecordModal',
  components: {
    PackagingWorkModal,
    PackagingWorkForm
  },
  data() {
    return {
      title: '',
      width: 1100,
      visible: false,
      disableSubmit: false,
      dataSource: [],
      tableLoading: false,
      detailColumns: [
        // {
        //   title: '#',
        //   dataIndex: '',
        //   key: 'rowIndex',
        //   width: 40,
        //   align: 'center',
        //   fixed: 'left',
        //   customRender: function (t, r, index) {
        //     return parseInt(index) + 1
        //   },
        // },
        {
          title: '单据类型',
          align: 'center',
          dataIndex: 'entryType_dictText'
        },
        {
          title: '包装入库单号',
          align: 'center',
          dataIndex: 'entryNumber'
        },
        {
          title: '物料编码',
          align: 'center',
          dataIndex: 'materialCode'
        },
        {
          title: '物料名称',
          align: 'center',
          dataIndex: 'materialName'
        },
        {
          title: '规格型号',
          align: 'center',
          dataIndex: 'specificationModel'
        },
        {
          title: '单位',
          align: 'center',
          dataIndex: 'unit_dictText'
        },
        // {
        //   title: '数量',
        //   align: 'center',
        //   dataIndex: 'quantity'
        // },
        {
          title: '包装数量',
          align: 'center',
          dataIndex: 'packageTotalQuantity'
        },
        {
          title: '生产日期',
          align: 'center',
          dataIndex: 'productionDate',
          customRender: function(text) {
            return !text ? '' : (text.length > 10 ? text.substr(0, 10) : text)
          }
        },
        {
          title: '保质期（天）',
          align: 'center',
          dataIndex: 'shelfLife'
        },
        {
          title: '限用日期',
          align: 'center',
          dataIndex: 'expirationDate',
          customRender: function(text) {
            return !text ? '' : (text.length > 10 ? text.substr(0, 10) : text)
          }
        },
        {
          title: '批次号',
          align: 'center',
          dataIndex: 'batchNumber'
        },
        {
          title: '物料批号',
          align: 'center',
          dataIndex: 'materialBatchNumber'
        },
        {
          title: '储位',
          align: 'center',
          dataIndex: 'storageLocation_dictText'
        },
        {
          title: '入库日期',
          align: 'center',
          dataIndex: 'entryDate',
          customRender: function(text) {
            return !text ? '' : (text.length > 10 ? text.substr(0, 10) : text)
          }
        },
        {
          title: '库龄（天）',
          align: 'center',
          dataIndex: 'inventoryAge'
        },
        {
          title: '质检状态',
          align: 'center',
          dataIndex: 'qualityInspectionStatus_dictText'
        },
        {
          title: '操作人',
          align: 'center',
          dataIndex: 'createBy_dictText'
        },
        {
          title: '操作时间',
          align: 'center',
          dataIndex: 'createTime',
          customRender: function(text) {
            return !text ? '' : (text.length > 10 ? text.substr(0, 10) : text)
          }
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 60,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/wms/packaging-inbound-order/list',
        withdraw: '/wms/packaging-inbound-order/withdraw'
      },
      ipagination: {
        current: 1,
        pageSize: 30,
        pageSizeOptions: ['10', '20', '30'],
        showTotal: (total, range) => {
          return range[0] + '-' + range[1] + ' 共' + total + '条'
        },
        showQuickJumper: true,
        showSizeChanger: true,
        total: 0
      }
    }
  },
  methods: {
    detail() {
      this.fetchData()
      this.visible = true
    },
    // 处理分页变化
    handleTableChange(pagination) {
      this.ipagination.current = pagination.current;
      this.ipagination.pageSize = pagination.pageSize;
      this.fetchData();
    },
    fetchData() {
      this.tableLoading = true 
      const params = {
        pageNo: this.ipagination.current,
        pageSize: this.ipagination.pageSize
      }
      getAction(this.url.list, params).then((res) => {
        if (res.success) {
          this.dataSource = res.result.records
          this.dataSourceBack = this.dataSource
          if (res.result.total) {
            this.ipagination.total = res.result.total
          } else {
            this.ipagination.total = 0
          }
        } else {
          this.$message.warning(res.message)
        }
      }).finally(() => {
        this.tableLoading = false
      })
    },
    handleDetail(record) {
      this.$refs.packagingWorkModal.edit(record)
      this.$refs.packagingWorkModal.title = '详情'
      this.$refs.packagingWorkModal.disableSubmit = true
    },
    // 撤回
    handleWithdraw: function (record) {
      postAction('/wms/packaging-inbound-order/withdraw', { id: record.id })
        .then((res) => {
          if (res.success) {
            this.$message.success(res.message)
            this.$emit('ok')
            this.fetchData()
          } else {
            this.$message.warning(res.message)
          }
        })
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      this.submitCallback()
    },
    submitCallback() {
      this.$emit('ok')
      this.visible = false
    },
    handleCancel() {
      this.close()
    }
  }
}
</script>